﻿namespace Helper
{
    using System;
    using System.Collections.ObjectModel;
    using System.IO;

    public static class GlobalConfigHelper
    {
        public static bool GetBoolValue(string line)
        {
            return ("1" == GetStringValue(line));
        }

        public static void GetIntegerListValue(Collection<int> list, string line)
        {
            foreach (string str in GetStringValue(line).Split(new char[] { ',' }))
            {
                int integer = ContentHelper.GetInteger(str);
                if (-1 != integer)
                {
                    list.Add(integer);
                }
            }
        }

        public static int GetIntegerValue(string line)
        {
            return ContentHelper.GetInteger(GetStringValue(line));
        }

        public static string GetStringValue(string line)
        {
            return line.Substring(line.IndexOf('=') + 1);
        }

        public static GlobalConfigData ReadGlobalConfigData(string filename)
        {
            GlobalConfigData data = new GlobalConfigData();
            try
            {
                foreach (string str in File.ReadAllLines(filename))
                {
                    if (str.StartsWith("proxyserver=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.ProxyServer = GetStringValue(str);
                    }
                    else if (str.StartsWith("proxyport=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.ProxyPort = GetIntegerValue(str);
                    }
                    else if (str.StartsWith("proxyuser=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.ProxyUser = GetStringValue(str);
                    }
                    else if (str.StartsWith("proxypass=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.ProxyPass = GetStringValue(str);
                    }
                    else if (str.StartsWith("DelayConnect=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.ConnectDelayTime = GetIntegerValue(str);
                    }
                    else if (str.StartsWith("RepeatTime=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.RepeatTime = GetIntegerValue(str);
                    }
                    else if (str.StartsWith("TryTimes=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.TryTimes = GetIntegerValue(str);
                    }
                    else if (str.StartsWith("HttpTimeout=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.HttpTimeout = GetIntegerValue(str) * 0x3e8;
                    }
                    else if (str.StartsWith("HttpTryTimes=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.HttpTryTimes = GetIntegerValue(str);
                    }
                    else if (str.StartsWith("EnableLog=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.WriteToLogFile = GetBoolValue(str);
                    }
                    else if (str.StartsWith("MatchMidnight=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.MatchMidnight = GetStringValue(str);
                    }
                    else if (str.StartsWith("MidnightForce=", StringComparison.OrdinalIgnoreCase))
                    {
                        data.MidnightForce = GetIntegerValue(str);
                        data.MidnightForce = data.MidnightForce > 10 ? 10 : data.MidnightForce;
                    }
                }
            }
            catch (Exception exception)
            {
                TraceLog.Error(exception.Message, new object[0]);
                return null;
            }
            return data;
        }
    }
}

